package com.huike.stock_analysis.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;


import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;


import com.huike.stock_analysis.entity.Stock;
import com.huike.stock_analysis.entity.TechniqueInfo;

/**
 * 
 * @author mzLEI
 * @date 2020年7月2日
 */
@Repository
public class TechniqueDao {
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	
	
	/**
	 * 
	 * @param code
	 * @return stock
	 * 查询股票当天基本信息
	 */
	public Stock baseInfo(String code) {
		
		String sql = "SELECT * FROM `ads_jszb` WHERE `ads_id` = " + code + " ORDER BY `ads_date` DESC LIMIT 1";
		Stock stock = jdbcTemplate.queryForObject(sql, new RowMapper<Stock>(){

			@Override
			public Stock mapRow(ResultSet rs, int rowNum) throws SQLException {
				// TODO Auto-generated method stub
				Stock stock = new Stock();
				stock.setChange(rs.getDouble("ads_chg"));
				stock.setChangeV(rs.getDouble("ads_cha"));
				stock.setClose(rs.getDouble("ads_close"));
				stock.setCode(rs.getString("ads_id"));
				stock.setHight(rs.getDouble("ads_hight"));
				stock.setLow(rs.getDouble("ads_low"));
				stock.setMoney(rs.getDouble("ads_fallen"));
				stock.setName(rs.getString("ads_name"));
				stock.setOpen(rs.getDouble("ads_open"));
				stock.setVolume(rs.getDouble("ads_auction"));
				stock.setDate(rs.getDate("ads_date").toString());
				return stock;
			}
			
		});
		return stock;
	}
	
	/**
	 * 
	 * @param code
	 * @return stock
	 * 查询股票技术指标信息
	 */
	
	public List<TechniqueInfo> queryTech(String code){
		String sql = "SELECT * FROM `ads_jszb` WHERE `ads_id` = " + code + " ORDER BY `ads_date` DESC LIMIT 100";
		List<TechniqueInfo> techniqueInfos = jdbcTemplate.query(sql,  new RowMapper<TechniqueInfo>() {
			
			@Override
			public TechniqueInfo mapRow(ResultSet rs, int col) throws SQLException {
				TechniqueInfo techniqueInfo = new TechniqueInfo();
				double boll[] = {rs.getDouble("ads_boll_upper"), rs.getDouble("ads_boll"), rs.getDouble("ads_boll_lower")};
				techniqueInfo.setBoll(boll);
				techniqueInfo.setChange(rs.getDouble("ads_chg"));
				techniqueInfo.setChange5(rs.getDouble("ads_zdf5"));
				techniqueInfo.setDate(rs.getDate("ads_date"));
				double ema[] = {rs.getDouble("ads_ema12"), rs.getDouble("ads_ema26")};
				techniqueInfo.setema(ema);
				double kdj[] = {rs.getDouble("ads_kdj_k"), rs.getDouble("ads_kdj_d"), rs.getDouble("ads_kdj_j")};
				techniqueInfo.setKdj(kdj);
				double ma[] = {rs.getDouble("ads_ma5"), rs.getDouble("ads_ma10"), rs.getDouble("ads_ma20")};
				techniqueInfo.setMa(ma);
				techniqueInfo.setMacd(rs.getDouble("ads_macd"));
				techniqueInfo.setRsi(rs.getDouble("ads_rsi"));
				techniqueInfo.setRsv(rs.getDouble("ads_rsv"));
				techniqueInfo.setDif(rs.getDouble("ads_dif"));
				techniqueInfo.setAvrHistoryRatio(rs.getDouble("ads_hylsbdl"));
				techniqueInfo.setCjl(rs.getDouble("ads_cjl"));
				techniqueInfo.setHistoryRatio(rs.getDouble("ads_lsbdl"));
				return techniqueInfo;
			}
		});
		
		return techniqueInfos;
	
	}
	
	/**
	 * 
	 * @param code
	 * @return stock
	 * 日k数据
	 */
	
	public List<Stock> dayK(String code){
		String sql = "SELECT * FROM `ads_jszb` WHERE `ads_id` = " + code + " ORDER BY `ads_date` DESC LIMIT 100";
		List<Stock> stocks = jdbcTemplate.query(sql,  new RowMapper<Stock>() {
			
			@Override
			public Stock mapRow(ResultSet rs, int col) throws SQLException {
				Stock stock = new Stock();
				stock.setOpen(rs.getDouble("ads_open"));
				stock.setClose(rs.getDouble("ads_close"));
				stock.setHight(rs.getDouble("ads_hight"));
				stock.setLow(rs.getDouble("ads_low"));
				stock.setDate(rs.getDate("ads_date").toString());
				return stock;
			}
		});
		
		return stocks;
	
	}
	
	
	
	
}
